package steven.sdc.appPoker;


import steven.sdc.appPoker.Fragment.FragmentBangDiem;
import steven.sdc.appPoker.Fragment.FragmentCauHinh;
import steven.sdc.appPoker.Fragment.FragmentChiTiet;
import steven.sdc.appPoker.Fragment.FragmentKetQua;
import steven.sdc.appPoker.Fragment.FragmentLichSu;
import steven.sdc.appPoker.Fragment.FragmentLuatChoi;
import steven.sdc.appPoker.Fragment.FragmentLuuTroChoi;
import steven.sdc.appPoker.Fragment.FragmentNguoiChoi;
import steven.sdc.appPoker.Fragment.FragmentTaoGame;
import steven.sdc.appPoker.Fragment.FragmentThongTin;

import com.amurani.slidingmenu.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

	ImageButton menu_button; 
	View btn_taogame,btn_lichsu, btn_luatchoi, btn_luutrochoi,btn_bangdiem,btn_nguoichoi,btn_thongtin,btn_thoat;
	TextView paragraph,tgame;
	LinearLayout content, menu;
	LinearLayout.LayoutParams contentParams;
	TranslateAnimation slide;
	int marginX, animateFromX, animateToX = 0;
	boolean menuOpen = true;
	Fragment frag;
//	String[] paragraphs; 
	
	public void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		frag =new FragmentLichSu();
		getSupportFragmentManager().beginTransaction().add(R.id.fragment_content, frag,null).addToBackStack(null).commitAllowingStateLoss();
		menu = (LinearLayout)findViewById(R.id.menu);
		
		content = (LinearLayout)findViewById(R.id.content);
		contentParams = (LinearLayout.LayoutParams)content.getLayoutParams();
		contentParams.width = getWindowManager().getDefaultDisplay().getWidth();	// Ensures constant width of content during menu sliding
		contentParams.leftMargin = -(menu.getLayoutParams().width);		// Position the content at the start of the screen
		content.setLayoutParams(contentParams);
		
		menu_button = (ImageButton)findViewById(R.id.menu_button);
		menu_button.setOnClickListener(this);
		
//		paragraph = (TextView)findViewById(R.id.paragraph);
//		paragraph.setText(paragraphs[0]);
		
		btn_taogame = findViewById(R.id.tao_game);
		btn_taogame.setOnClickListener(this);
		
		btn_lichsu = findViewById(R.id.lich_su);
		btn_lichsu.setOnClickListener(this);
		
//		btn_bangdiem = findViewById(R.id.bang_diem);
//		btn_bangdiem.setOnClickListener(this);
		
		btn_luatchoi = findViewById(R.id.luat_choi);
		btn_luatchoi.setOnClickListener(this);	
		
		btn_nguoichoi = findViewById(R.id.nguoi_choi);
		btn_nguoichoi.setOnClickListener(this);
		
		btn_luutrochoi = findViewById(R.id.luu_tro_choi);
		btn_luutrochoi.setOnClickListener(this);
		
		btn_thongtin = findViewById(R.id.thong_tin);
		btn_thongtin.setOnClickListener(this);
		
		btn_thoat = findViewById(R.id.thoat);
		btn_thoat.setOnClickListener(this);	
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			if(menuOpen) {
				slideMenuIn(0, -(menu.getLayoutParams().width), -(menu.getLayoutParams().width)); 	// Pass slide in paramters
				menuOpen = false;
				return true;
			}
		}
		return super.onKeyDown(keyCode, keyEvent);
	}
	
	public void onClick(View v) {
	switch(v.getId()) {
		
		case R.id.tao_game:
		{
			
			frag =new FragmentTaoGame();
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag,null).commitAllowingStateLoss();
			//tgame.setText("Game Hiện Tại");
		}
		
			break;
		case R.id.lich_su:
		{
		//	paragraph.setText(paragraphs[1]);
			frag =new FragmentLichSu();
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag,null).commitAllowingStateLoss();
		}
			break;
		case R.id.nguoi_choi:
		{
		//	paragraph.setText(paragraphs[2]);
			frag =new FragmentNguoiChoi();
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag,null).commitAllowingStateLoss();
		}break;
		case R.id.luu_tro_choi:
		{
		//	paragraph.setText(paragraphs[3]);
			frag=new FragmentLuuTroChoi();
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag,null).commitAllowingStateLoss();
		}break;
		case R.id.luat_choi:
		{
			frag = new FragmentLuatChoi();
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag,null).commitAllowingStateLoss();
		}break;
		case R.id.thong_tin:
		{
			frag = new FragmentThongTin();
			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag,null).commitAllowingStateLoss();
		}break;
//		case R.id.bang_diem:
//		{
//			frag = new FragmentBangDiem();
//			getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, frag, null).commitAllowingStateLoss();
//		}break;
		case R.id.thoat:
		{
			AlertDialog.Builder b=new AlertDialog.Builder(MainActivity.this);
			b.setTitle("THOÁT");
			b.setMessage("Bạn Chắc Chắn Muốn Thoát Game ?");
			b.setPositiveButton("Có",new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				MainActivity.this.finish();
				 //System.exit(0);
			}
		});
		b.setNegativeButton("Không",new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		b.create().show();
           
		}break;
		
	}
		if(contentParams.leftMargin == -(menu.getLayoutParams().width)) {	// Menu is hidden (slide out parameters)
			animateFromX = 0;
			animateToX = (menu.getLayoutParams().width);
			marginX = 0;
			menuOpen = true;
		} else {	// Menu is visible (slide in parameter)
			animateFromX = 0;
			animateToX = -(menu.getLayoutParams().width);
			marginX = -(menu.getLayoutParams().width);
			menuOpen = false;
		}
		slideMenuIn(animateFromX, animateToX, marginX);
	}
		
	public void slideMenuIn(int animateFromX, int animateToX, final int marginX){
		slide = new TranslateAnimation(animateFromX, animateToX, 0, 0);
		slide.setDuration(300);
		slide.setFillEnabled(true);
		slide.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation animation) {		// Make movement of content permanent after animation has completed 
				contentParams.setMargins(marginX, 0, 0, 0);			// by positioning its left margin
				content.setLayoutParams(contentParams);
			}

			public void onAnimationRepeat(Animation animation) { }
			public void onAnimationStart(Animation animation) { }
		});
		content.startAnimation(slide);		// Slide menu in or out
	}
}